﻿/*************************************
Function：XML序列化
 Version：1.0
    Date：2012-04-28
  Author：
   Email：
 WebSite：
*************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Wind.Compressor.Business
{
    ///  <summary>
    ///  序列化与反序列化
    ///  </summary>
    public class SerializerHelper
    {
        ///  <summary>
        ///  将文件反序列化成对象
        ///  </summary>
        ///  <param  name="type">对象类型</param>
        ///  <param  name="path">文件路径</param>
        ///  <returns></returns>
        public static object XmlDeserializerFormFile(Type type, string path)
        {
            using (XmlTextReader xmlReader = new XmlTextReader(path))
            {
                object obj = new XmlSerializer(type).Deserialize(xmlReader);                 
                xmlReader.Close();
                return obj;
            }
        }


        ///  <summary>
        ///  将字符串内容反序列化成对象
        ///  </summary>
        ///  <param  name="type">对象类型</param>
        ///  <param  name="serializeText">被序列化的文本</param>
        ///  <returns></returns>
        public static object XmlDeserializerFormText(Type type, string serializeText)
        {
            using (StringReader reader = new StringReader(serializeText))
            {                
                object obj = new XmlSerializer(type).Deserialize(reader);
                reader.Close();
                return obj;
            }
        }




        ///  <summary>
        ///  将目标对象序列化成XML到文件中
        ///  </summary>
        ///  <param  name="target"></param>
        ///  <param  name="path"></param>
        public static void XmlSerializerToFile(object target, string path)
        {
            //XmlTextWriter  writer  =  new  XmlTextWriter(path,  Encoding.UTF8);
            using (StreamWriter writer = new StreamWriter(path))
            {
                new XmlSerializer(target.GetType()).Serialize((StreamWriter)writer, target);
                writer.Flush();
                writer.Close();
            }
        }

        ///  <summary>
        ///  将目标对象序列化成完整的XML文档
        ///  </summary>
        ///  <param  name="target"></param>
        ///  <returns></returns>
        public static string XmlSerializerToXml(object target)
        {
            return XmlSerializerToText(target, false);
        }


        ///  <summary>
        ///  将目标对象序列化成XML文档内容(去除声明属性)
        ///  </summary>
        ///  <param  name="target"></param>
        ///  <returns></returns>
        public static string XmlSerializerToText(object target)
        {
            return XmlSerializerToText(target, true);
        }


        ///  <summary>
        ///  将目标对象序列化成XML文档
        ///  </summary>
        ///  <param  name="target"></param>
        ///  <param  name="isText">是否去除声明属性</param>
        ///  <returns></returns>
        private static string XmlSerializerToText(object target, bool isText)
        {
            if (target == null)
                return string.Empty;

            using (StringWriter writer = new StringWriter())
            {
                new XmlSerializer(target.GetType()).Serialize((TextWriter)writer, target);
                StringBuilder sb = writer.GetStringBuilder();
                writer.Close();
                if (isText)
                {
                    sb.Replace("<?xml  version=\"1.0\"  encoding=\"utf-16\"?> ", "");
                    sb.Replace("  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
                }
                else
                {
                    sb.Replace("utf-16", "utf-8");
                }
                return sb.ToString();
            }
        }
    }
}
